package com.example.supermarket.service;

import com.example.supermarket.entity.Admin;
import com.example.supermarket.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>管理员模块逻辑处理层</p>
 * @author Bosen
 * @date 2021/10/8 17:29
 */
@Service
public class AdminHomeService {

    @Autowired
    AdminMapper adminMapper;

    /**
     * <p>检查登录信息是否正确</p>
     * @param name 用户昵称
     * @param password 密码
     */
    public boolean checkLoginInfo(HttpServletRequest request, String name, String password) {
        // 获取“id=1”的管理员信息
        Admin admin = adminMapper.findById(1);
        // 昵称和密码匹配返回true，否则返回false
        if (admin.getName().equals(name) &&
                admin.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
            // 使用session存储登录信息
            request.getSession().setAttribute("admin", admin);
            return true;
        }
        return false;
    }





    /**
     * <p>退出登录</p>
     */
    public void logout(HttpServletRequest request) {
        request.getSession().setAttribute("admin", null);
    }
}
